<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\common\lib\Show;
use think\facade\Request;

class Image extends BaseController
{
    public function upload()
    {
        if (!Request::isPost()) {
            return Show::error('非法请求');
        }

        $file = Request::file('file');

        $filename = \think\facade\Filesystem::disk('public')->putFile('image', $file);

        if (!$filename) return Show::error('图片上传失败');

        $imagePath = ['image' => "/upload/$filename"];

        return Show::success($imagePath, '图片上传成功');
    }

    public function layUpload()
    {
        if (!Request::isPost()) {
            return Show::error('非法请求');
        }

        $file = Request::file('file');

        $filename = \think\facade\Filesystem::disk('public')->putFile('image', $file);

        if (!$filename) return json(['code' => 1, 'data' => []], 200);

        return json(['code' => 0, 'data' => ['src' => "/upload/$filename"]], 200);
    }
}
